package com.ruoyi.rural.controller;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rural.domain.EpidemicMapData;
import com.ruoyi.rural.domain.RiskArea;
import com.ruoyi.rural.domain.RuralUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/epidemicData")
public class epidemicDataController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 疫情地图数据
     */
    @GetMapping("/epidemicMapData")
    public AjaxResult epidemicMapData()
    {
        String mapData = "";
        String path = "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf";
        if (redisTemplate.hasKey("riskAreaData")){
            mapData = (String) redisTemplate.opsForValue().get("epidemicMapData");
            if (StringUtils.isEmpty(mapData)){
                mapData = getApiData(path,"epidemicMapData");
            }
        }else {
            mapData = getApiData(path,"epidemicMapData");
            if (StringUtils.isEmpty(mapData)){
                return AjaxResult.error("接口请求数据错误！");
            }
        }
        return AjaxResult.success(mapData);
    }

    public String getApiData(String path, String name){
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("GET");//GET和POST必须全大写
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";
            String response = "";
            while ((str = br.readLine()) != null) {
                str=new String(str.getBytes(),"UTF-8");//解决中文乱码问题
                redisTemplate.opsForValue().set(name, str, 1, TimeUnit.DAYS);
                response = str;
            }
            is.close();
            conn.disconnect();
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
